package seedling_record

import (
	_ "app/api/schema"
	rich_err "app/error"
	_ "app/models"
	"app/service/seedling_record"
	"app/utils"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Init(group *gin.RouterGroup) {
	Gets(group)
	Get(group)
	Create(group)
	Update(group)
	Delete(group)
}

// @Summary 获取出苗记录列表
// @Tags seedling_record
// @Produce json
// @Description.markdown desc_common_page
// @param page_size query integer false "用于分页的每页最大元素数量" minimum(1)
// @param page_index query integer false "请求的页码" minimum(1)
// @param order query string false "需要排序的字段,如需倒序,字段名前面加 `-`"
// @Success 200 {object} schema.Response{data=[]models.SeedlingRecordDetail}
// @Failure 400,500 {object} schema.ResponseErr
// @Router /api/v1/seedling_record [get]
func Gets(group *gin.RouterGroup) {
	group.GET("/", func(context *gin.Context) {
		data, pager, err := seedling_record.Gets(context)
		utils.GinJSONPageWithRichErr(context, data, pager, err)
	})
}

// @Summary 获取指定出苗记录信息
// @Tags seedling_record
// @Produce json
// @Success 200 {object} schema.Response{data=models.SeedlingRecordDetail}
// @Failure 404,500 {object} schema.ResponseErr
// @Param id path string true "出苗记录ID"
// @Router /api/v1/seedling_record/{id} [get]
func Get(group *gin.RouterGroup) {
	group.GET("/:id", func(context *gin.Context) {
		id, err := strconv.Atoi(context.Param("id"))
		if err != nil {
			utils.GinJSONWithRichErr(context, nil, rich_err.NewError(rich_err.RequestInvalid, err))
			return
		}
		data, richErr := seedling_record.Get(strconv.Itoa(id))
		utils.GinJSONWithRichErr(context, data, richErr)
	})
}

// @Summary 创建出苗记录
// @Tags seedling_record
// @Accept json
// @Produce json
// @Param req body models.SeedlingRecordCore true "出苗记录核心信息"
// @Success 200 {object} schema.Response{data=models.SeedlingRecordDetail}
// @Failure 400,500 {object} schema.ResponseErr
// @Router /api/v1/seedling_record [post]
func Create(group *gin.RouterGroup) {
	group.POST("/", func(context *gin.Context) {
		data, richErr := seedling_record.Create(context)
		utils.GinJSONWithRichErr(context, data, richErr)
	})
}

// @Summary 修改出苗记录
// @Tags seedling_record
// @Accept json
// @Produce json
// @Param req body models.SeedlingRecordDelta true "出苗记录修改信息"
// @Param id path string true "出苗记录ID"
// @Success 200 {object} schema.Response{data=models.SeedlingRecordDetail}
// @Failure 400,500 {object} schema.ResponseErr
// @Router /api/v1/seedling_record/{id} [patch]
func Update(group *gin.RouterGroup) {
	group.PATCH("/:id", func(context *gin.Context) {
		_, err := strconv.Atoi(context.Param("id"))
		if err != nil {
			utils.GinJSONWithRichErr(context, nil, rich_err.NewError(rich_err.RequestInvalid, err))
			return
		}
		data, richErr := seedling_record.Update(context)
		utils.GinJSONWithRichErr(context, data, richErr)
	})
}

// @Summary 删除出苗记录
// @Tags seedling_record
// @Produce json
// @Param id path string true "出苗记录ID"
// @Success 200 {object} schema.Response
// @Failure 400,500 {object} schema.ResponseErr
// @Router /api/v1/seedling_record/{id} [delete]
func Delete(group *gin.RouterGroup) {
	group.DELETE("/:id", func(context *gin.Context) {
		_, err := strconv.Atoi(context.Param("id"))
		if err != nil {
			utils.GinJSONWithRichErr(context, nil, rich_err.NewError(rich_err.RequestInvalid, err))
			return
		}
		richErr := seedling_record.Delete(context)
		utils.GinJSONWithRichErr(context, nil, richErr)
	})
}
